﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/App.Master" Inherits="System.Web.Mvc.ViewPage<Ornament.MemberShip.Permissions.Permission>" %>

<asp:Content ID="Head" ContentPlaceHolderID="HeadContent" runat="server">

    <script src="/Scripts/jquery.json.min.js" type="text/javascript"></script>

    <script src="/Scripts/jQuery.easyValidate.js" type="text/javascript"></script>

    <script src="/Scripts/jquery.bindTools.js" type="text/javascript"></script>

    <script type="text/javascript">
     
        $(document).ready(function() {
          
            $("#previous").button().click(function(){
                $("#operatorValue").val(getOperatorValue());
                $("#forward").val("false");
                $("#form1")[0].submit();
            });
            
            $("#save").button().click(function(){
                $("#operatorValue").val(getOperatorValue());
                $("#forward").val("true");                
            });                        
            
            $('#cancel').button().click(function() {
                if (confirm("选择离开会失去当前数据.点击'确认'退出")) {
                    location = '<%=Url.Action("index")%>';
                }
            });            
            $("#OperatorsList :checkbox").click(function() {
            
                var check = this.checked;
                var beCheckValue = parseInt($(this).val());
                
                $(this).siblings(":checkbox").each(function(){                
                    if (beCheckValue == 0){
                        this.checked = !check;
                    }
                    var checkValue = parseInt($(this).val());                
                    if (beCheckValue >= checkValue) {
                        var include = hasPermission(beCheckValue, checkValue);
                        if (include && check) {
                            this.checked = true;
                        }
                    }
                    else {
                        var include = hasPermission(checkValue, beCheckValue);
                        if (include && !check) {
                            this.checked = false;
                        }
                    }
                });
            });           
            
            SetOperatorsValue(<%=this.Model.Operator%>);
             
         
           
        });         
      
  
        function getOperatorValue() {
            var operator = 0;
            $("#OperatorsList :checkbox").each(function() {
                if (this.checked) {
                    operator |= parseInt($(this).val());
                }
            });
                return operator;
            }
    
        function SetOperatorsValue(permissions) {
            $("#OperatorsList :checkbox").each(function() {
                var theValue = parseInt(this.value);
                var value = theValue & permissions;
                this.checked = (value == theValue) && (permissions > theValue) && (theValue != 0);
            });
        }  
        
    </script>

    <style type="text/css">
        #editPanel
        {
            width: 100%;
        }
        /* clear cssform label effective*/#OperatorsList label
        {
            float: none;
            margin-right: 0em;
            text-align: left;
            width: 4em;
        }
    </style>
</asp:Content>
<asp:Content ID="headTitle" ContentPlaceHolderID="PageTitle" runat="server">
    <%=Model.Name %>
</asp:Content>
<asp:Content ID="pageTitle" ContentPlaceHolderID="TitleContent" runat="server">
    <%=Model.Name %>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <form action="<%=Url.Action("Save",new {id=this.Model.Id==Guid.Empty?"":this.Model.Id.ToString()})%>"
    id="form1" method="post">
    <input id="forward" name="forward" type="hidden" />
    <input id="operatorValue" name="operatorValue" type="hidden" />
     <input type="hidden" id="pathOfResourceType" value="<%=ViewData["pathOfResourceType"] %>" />
    <fieldset id="editPanel" class="cssform">
        <legend>许可证信息</legend>
        <p>
            <label>
                名称:
            </label>
            <%=Html.TextBox("Name") %>
        </p>
        <p>
            <label>
                说明:
            </label>
            <%=Html.TextArea("Comment")%>
        </p>
        <div class="fieldItem">
            <label>
                操作:</label>
            <div id="OperatorsList" class="data">
                <%
                    var op = Ornament.EnumHelper.GetDescriptionList(Type.GetType(this.Model.Resource.OperatorType));
                    foreach (var key in op.Keys)
                    {           
            
                %>
                <input type="checkbox" id="<%=op[key]%>" value="<%=System.Convert.ToInt32(op[key])%>" />
                <label for="<%=op[key]%>">
                    <%=key%></label>
                <br />
                <%} 
                %>
            </div>
        </div>
    </fieldset>
    <div>
        <button id="previous" type="button">
            上一步</button>
        <button id="save" type="submit">
            完成</button>
        <button id="cancel" type="button">
            取消</button>
    </div>
    </form>
</asp:Content>
